{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python 准备\n",
    "\n",
    "```{note}\n",
    "- 不要在程序中，行开头处增加空格。空格在Python 中有缩进的含义。\n",
    "\n",
    "- 符号都是**英文符号**，不是中文。比如：(,”\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 注释\n",
    "\n",
    "- 单行注释: 以“#”开头\n",
    "- 多行注释使用三个单引号对(''' ''')或三个双引号对(\"\"\" \"\"\") \n",
    "\n",
    "![](https://tingwen-pic.oss-cn-beijing.aliyuncs.com/img/20220228010605.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def h(a,b):\n",
    "    '''\n",
    "    @description: use to do summation\n",
    "    @param: two inputs\n",
    "    '''\n",
    "    # sum\n",
    "    c = a+b \n",
    "    return c # 返回结果\n",
    "\n",
    "h(1,2)\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```{note}\n",
    "- ctrl + /: 单行注释快捷键\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python语法格式缩进\n",
    "- 使用缩进表示代码块\n",
    "  - 强制缩进：需要写出格式化的代码\n",
    "- 当语句以`:`结尾时，缩进的语句视为代码块\n",
    "- 约定俗成，4个空格为一个缩进; \n",
    "  \n",
    "\n",
    "```{note}\n",
    "- 目前，常用的编辑器一般设置成：tab 制表符就是4个空格。\n",
    "- 但要避免使用tab与空格混合的缩进风格。\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 成绩分组\n",
    "\n",
    "import numpy as np\n",
    "grade = np.random.randint(50,101,10)\n",
    "\n",
    "for i in grade:\n",
    "    if i>=80:\n",
    "        print('优秀')\n",
    "    elif i>=60:\n",
    "        print('及格')\n",
    "    else:\n",
    "        print('fail')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python的输入与输出\n",
    "\n",
    "- input和output统称为IO，是命令行下最基本的输入和输出\n",
    "\n",
    "  - `input()`输入: 会把用户通过键盘输入的任何值都作为**字符串**来存储变量。\n",
    "  - `print()`: 输出\n",
    "  \n",
    "  ```{note}\n",
    "  注意：print 默认输出是换行的，**如果要实现不换行需要在变量末尾加上`end=\"\"`** \n",
    "  ```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-01T15:27:50.967932Z",
     "start_time": "2022-03-01T15:27:42.323465Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "what's your name:may\n",
      "may\n"
     ]
    }
   ],
   "source": [
    "name = input(\"what's your name:\")\n",
    "print(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-01T15:29:43.946868Z",
     "start_time": "2022-03-01T15:29:43.927919Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "--------------------\n",
      "01234"
     ]
    }
   ],
   "source": [
    "for i in range(5):\n",
    "    print(i)\n",
    "\n",
    "print('-'*20)\n",
    "\n",
    "for i in range(5):\n",
    "    print(i,end=\"\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 变量\n",
    "\n",
    "- 变量可以理解为是存储数据的容器\n",
    "- 定义变量的格式： 变量名 = 数据 （变量初始化）\n",
    "    - 变量命名尽量有含义，便于理解\n",
    "- 变量占用内存空间\n",
    "\n",
    "- 常用的数据类型：int, float, str, bool, list, dict, tuple, set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-01T15:41:36.048110Z",
     "start_time": "2022-03-01T15:41:36.033150Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "April econ 100\n"
     ]
    }
   ],
   "source": [
    "major = 'econ'\n",
    "student_name = 'April'\n",
    "grade = 100\n",
    "print(student_name,major, grade)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 变量命名规则\n",
    "\n",
    "- 变量名由 字母、数字和下划线构成\n",
    "    - 只有这三种，且不能用数字开头 （第一个字符必须是字母或下划线）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-01T15:45:09.637853Z",
     "start_time": "2022-03-01T15:45:09.619903Z"
    }
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (Temp/ipykernel_3756/34172116.py, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"C:\\Users\\illus\\AppData\\Local\\Temp/ipykernel_3756/34172116.py\"\u001b[1;36m, line \u001b[1;32m2\u001b[0m\n\u001b[1;33m    22class = 'econ'\u001b[0m\n\u001b[1;37m      ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "# 错误的命名\n",
    "22class = 'econ'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 变量命名方法：\n",
    "    - 驼峰命名法\n",
    "        - 小驼峰：第一个单词首字母小写，其它单词首字母大写\n",
    "        - 大驼峰：每个单词首字母都大写\n",
    "        \n",
    "    - 下划线命名法：单词都使用小写，单词之间使用下划线进行分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-01T15:49:32.468894Z",
     "start_time": "2022-03-01T15:49:32.452935Z"
    }
   },
   "outputs": [],
   "source": [
    "stu_name = 'April'  # 下划线命名法\n",
    "stuName = \"May\" # 小驼峰\n",
    "StuName = \"June\"  # 大驼峰"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- Python 中的标识符是区分大小写， sxt 和SXT 是不同的变量\n",
    "- Python关键字（Keywords）: 系统关键字不能用作任何变量名称\n",
    "    - 【操作】使用Python 帮助系统查看关键字\n",
    "        - `help()`\n",
    "        - help> keywords\n",
    "    - ['False','None','True','and','as','assert','async','await','break','class','continue','def','del',\n",
    "    'elif','else','except','finally','for','from','global','if','import','in','is','lambda','nonlocal',\n",
    "    'not','or','pass','raise','return','try','while','with','yield']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 函数、类、模块等约定俗成的命名规则：\n",
    "![](https://tingwen-pic.oss-cn-beijing.aliyuncs.com/img/20220301235916.png)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 程序分行\n",
    "\n",
    "- 一行程序长度是没有限制的，但是为了可读性更强，通常将一行比较长的程序分为多行。这时，我们可以使用`\\`行连接符，把它放在行结束的地方。Python 解释器仍然将它们解释为同一行。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-01T16:02:08.642707Z",
     "start_time": "2022-03-01T16:02:08.622758Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]\n"
     ]
    }
   ],
   "source": [
    "a = [10,20,30,40,\\\n",
    "50,60,70,\\\n",
    "80,90,100]\n",
    "\n",
    "print(a)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 代码风格\n",
    "\n",
    "- Python 官方推荐的PEP-8 代码风格，参考：\n",
    "https://www.python.org/dev/peps/pep-0008/\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```{admonition}  The Zen of Python, by Tim Peters\n",
    "- Beautiful is better than ugly.\n",
    "- Explicit is better than implicit.\n",
    "- Simple is better than complex.\n",
    "- Complex is better than complicated.\n",
    "- Flat is better than nested.\n",
    "- Sparse is better than dense.\n",
    "- Readability counts.\n",
    "- Special cases aren't special enough to break the rules.\n",
    "- Although practicality beats purity.\n",
    "- Errors should never pass silently.\n",
    "- Unless explicitly silenced.\n",
    "- In the face of ambiguity, refuse the temptation to guess.\n",
    "- There should be one-- and preferably only one --obvious way to do it.\n",
    "- Although that way may not be obvious at first unless you're Dutch.\n",
    "- Now is better than never.\n",
    "- Although never is often better than *right* now.\n",
    "- If the implementation is hard to explain, it's a bad idea.\n",
    "- If the implementation is easy to explain, it may be a good idea.\n",
    "- Namespaces are one honking great idea -- let's do more of those!\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "b3ba2566441a7c06988d0923437866b63cedc61552a5af99d1f4fb67d367b25f"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
